package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

/**
 * @author zhouwei
 * @date 2020-01-02 10:49
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    private Result result;
    /**
     *分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            String queryString = queryPageBean.getQueryString();
            if(queryString!=null&&queryString.length()>0){
                String replace = queryString.replace(" ", "");
                queryPageBean.setQueryString(replace);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 查询检查组数据
     * @return
     */
    @RequestMapping("/findCheckGroup")
    public Result findById(){
        List<CheckGroup> list;
        try {
            list=setmealService.findCheckGroup();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }


    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try {
        //获取原始文件名，后缀名，fileName=uuid+后缀名
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(index);
        String fileName=UUID.randomUUID().toString()+substring;
        //fileName上传到七牛云，并返回fileName给前端
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //将图片传入redis，基于redis集合,未入数据库
            //jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 添加setmeal
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
            //图片名存入数据库，添加成功就加入小集合
            //jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }
}
